function FunNodePanel(){
}

FunNodePanel.instance = FunNodePanel;

FunNodePanel.faceUrl = "funNodeController";

/**
 * 功能树表格
 */
FunNodePanel.funNodeTreegrid = null;

/**
 * 管理功能窗口
 */
FunNodePanel.funNodeEditWindow = null;

/**
 * 下拉选择树
 */
FunNodePanel.funNodeTreeselect = null;

/**
 * 下拉选择框
 */
FunNodePanel.funComboBox = null;

/**
 * 图标选择框
 */
FunNodePanel.funIconCombobox = null;

/**
 *  数据暂存
 */
FunNodePanel.data = [];

//初始化
FunNodePanel.init = function(){
	FunNodePanel.loadToolbar();
	FunNodePanel.funNodeTreegrid = mini.get("funNodeTreegrid");
	FunNodePanel.funNodeEditWindow = mini.get("funNodeEditWindow");
	FunNodePanel.funNodeTreeselect = mini.get("funNodeTreeselect");
	FunNodePanel.funComboBox = mini.get("funComboBox");
	FunNodePanel.funIconCombobox = mini.get("funIconCombobox");
	FunNodePanel.renderer();
	FunNodePanel.reload();
};

/**
 * 加载表格数据
 */
FunNodePanel.reload = function(){
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/" +FunNodePanel.faceUrl,
		data: {
			method: "find"
		},
		success: function(json) {
			FunNodePanel.data = json;
			FunNodePanel.funNodeTreegrid.loadData(json);
		}
	});
};

/**
 * 加载按钮
 */
FunNodePanel.loadToolbar = function() {
	var array = new Array("system_fun_add", "system_fun_edit", "system_fun_del",
			"system_fun_refresh");
	var array2 = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < array2.length; i++) {
		mini.get(array[i]).setVisible(array2[i]);
	}
};

/**
 * 加载下拉选择树的数据
 */
FunNodePanel.loadTreeSelect = function(){
	var fns = [];
	for ( var i = 0; i < FunNodePanel.data.length; i++) {
		fns.push(FunNodePanel.data[i]);
	}
	FunNodePanel.funNodeTreeselect.setData(fns);
};

/**
 * 加载下拉选择树的数据
 */
FunNodePanel.loadFunComboBox = function(){
	FunNodePanel.funComboBox.setData(XMPanel.funType);
};

/**
 * 加载图标选择树的数据
 */
FunNodePanel.loadIconComboBox = function(){
	var iconValue = XMPanel.iconType;
	for ( var i = 0; i < iconValue.length; i++) {
		iconValue[i].value =  iconValue[i].id;
	}
	FunNodePanel.funIconCombobox.setData(iconValue);
};

/**
 * 图标下拉列表值改变
 */
FunNodePanel.iconComBValueChange = function(e){
	FunNodePanel.funIconCombobox.setText(e.selected.id);
};

/**
 * 下拉列表值改变事件
 */
FunNodePanel.treeSelectChanged = function(){
	var textCls = mini.get("funNodeEditWindowPfunId");
	var idValue = FunNodePanel.funNodeTreeselect.getValue();
	
	textCls.setValue(idValue);
};

/**
 * 对表格数据渲染
 */
FunNodePanel.renderer = function(){
	FunNodePanel.funNodeTreegrid.on("drawcell", function (e){
		var  field = e.field, value = e.value;
		if(field == "funIcon"){e.cellHtml = "<div class='" + value + "' style='padding-left:20px;'>" + value + "</div>";}
		if(field == "funType"){
			if(value == "0"){e.cellHtml = "<div style='color: red'>功能模块</div>";};
			if(value == "1"){e.cellHtml = "<div style='color: blue'>功能节点</div>";};
			if(value == "2"){e.cellHtml = "<div style='color: green'>功能按钮</div>";};
		}
	});
};

/**
 * 关闭编辑功能窗口
 */
FunNodePanel.closeButton = function(){
	FunNodePanel.funNodeEditWindow.hide();
};

/**
 * 新增功能按钮事件
 */
FunNodePanel.add = function(){
	
	if (!MiniuiTools.hasPurview("system_fun_add")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}

	var data = FunNodePanel.funNodeTreegrid.getSelectedNode();
	var fdata = {
		'pfunId': '0',
		'pfunName': '顶层节点',
		'funType': '0'
	};

	if (data) {
		fdata.pfunId = data.funId;
		fdata.pfunName = data.name;
	}
	
	var form = new mini.Form("#funNodeEditform");
	form.clear();
	
	FunNodePanel.loadFunComboBox();
	FunNodePanel.loadIconComboBox();
	FunNodePanel.loadTreeSelect();
	FunNodePanel.funNodeTreeselect.setText(fdata.pfunName);
	var textCls = mini.get("funNodeEditWindowPfunId");
	textCls.setValue(fdata.pfunId);
	FunNodePanel.funNodeEditWindow.show();
};

/**
 * 编辑
 */
FunNodePanel.edit = function(){
	
	if (!MiniuiTools.hasPurview("system_fun_edit")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowData = FunNodePanel.funNodeTreegrid.getSelectedNode();
	
	if(!rowData){
		mini.alert("请选中要编辑的功能！");
	}else{
		var pfun = MiniuiTools.findTreeNode(rowData.pfunId, FunNodePanel.data);// FunNodePanel.findNode(data.pfunId);

		var form = new mini.Form("#funNodeEditform");
		form.clear();
		
		if (!pfun) pfun = {
			funId: '0',
			name: '顶层节点',
			pid: '-1'
		};
		
		rowData.pfunName = pfun.name;
		rowData.pfunId = pfun.funId;
		
		form.setData(rowData);
		FunNodePanel.funNodeTreeselect.setText(rowData.pfunName);
		FunNodePanel.loadFunComboBox();
		FunNodePanel.loadIconComboBox();
		FunNodePanel.funIconCombobox.setText(rowData.funIcon);
		FunNodePanel.loadTreeSelect();
		FunNodePanel.funNodeEditWindow.show();
	}
	
};

/**
 * 保存
 */
FunNodePanel.save = function(as){
	
	var form = new mini.Form("#funNodeEditform");
	
	form.validate();
    if (form.isValid() == false) return;
	
	var data = form.getData();
	data.method = "save";

	if (as == "yes") data.funId = "";
	if(!data.pfunId) data.pfunId	= "0";
	
	data.lslUser = gbOperateUser;
	data.lslAddress = gbOperateIp;

	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+FunNodePanel.faceUrl,
		data: data,
		success: function(text) {
			FunNodePanel.reload();
			//FunNodePanel.funNodeTreegrid.expandAll();
			//FunNodePanel.funNodeTreegrid.expandPath(FunNodePanel.funNodeTreegrid.getSelectedNode());
			if(as != "yes"){FunNodePanel.closeButton();}
			parent.ClientData.initAllFunNode();
			return;
		},
		error: function(obj, ertxt) {
			FunNodePanel.closeButton();
			mini.alert("初始化数据错误：" + ertxt);
		}
	});
};

/**
 * 删除
 */
FunNodePanel.del = function() {
	
	if (!MiniuiTools.hasPurview("system_fun_del")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}

	var rowData = FunNodePanel.funNodeTreegrid.getSelectedNode();

	if(!rowData){
		mini.alert("请选中要删除的记录！");
	}else{
		mini.confirm("确定要删除功能名称为  " + "<span style='color: red;'> " +rowData.name + "</span>" + "  的功能吗？", "删除功能",
	            function (action) {    
			  if (action == "ok") {
				  rowData.method = "delete";
				  rowData.lslUser = gbOperateUser;
				  rowData.lslAddress = gbOperateIp;
	
					$.ajax( {
						type: 'POST',
						dataType: 'json',
						url: __root+"/"+FunNodePanel.faceUrl,
						data: rowData,
						success: function(text) {
							FunNodePanel.reload();
							parent.ClientData.initAllFunNode();
							return;
						}
					});
			  }
		});
	}
};
